﻿using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace NfsBLL
{/// <summary>
    /// level
    /// </summary>
    public partial class LevelBLL
    {
        private readonly NfsDAL.LevelDAL dal = new NfsDAL.LevelDAL();
        public LevelBLL()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(NfsModel.level model, out int id)
        {
            return dal.Add(model, out id);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(NfsModel.level model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(Maticsoft.Common.PageValidate.SafeLongFilter(idlist, 0));
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public NfsModel.level GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public NfsModel.level GetModelByCache(int id)
        {

            string CacheKey = "levelModel-" + id;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (NfsModel.level)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<NfsModel.level> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<NfsModel.level> DataTableToList(DataTable dt)
        {
            List<NfsModel.level> modelList = new List<NfsModel.level>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                NfsModel.level model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  BasicMethod
        #region  自定义方法
        #region PC模糊查询
        public List<level> GetLevelBycondtion(int pageindex, int pagesize)
        {
            DataTable dt = dal.GetLevelBycondtion(pageindex, pagesize);
            return ConverToExcs.ConvertToEx<level>(dt);
        }
        public object GetLevelBycondtionCount()
        {
            return dal.GetLevelBycondtionCount();
        }
        #endregion
        #region 修改和添加用户判断用户信息是否有重复信息
        /// <summary>
        /// 重复判断
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="coding"></param>
        /// <returns></returns>
        public bool GetSingle(int id, string name, string coding)
        {
            return dal.GetSingle(id, name, coding);
        }
        #endregion
        #region 修改Code
        public bool UptLevel_code(int id, string Code)
        {
            return dal.UptLevel_code(id, Code);
        }
        #endregion

        #region 添加用户等级
        public bool InsertLevel(level model, string code)
        {

            bool flag = true;
            int agId = 0;

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    int ids;
                    flag = this.Add(model, out ids);
                    if (flag)
                    {
                        int Maxid = ids;
                        if (model.parent_id.ToString()!= "0")
                        {
                            string Lcode = code + "00" + Maxid.ToString();
                           flag=this.UptLevel_code(Maxid, Lcode);
                        }
                        else
                        {
                            string Lcode = "00" + Maxid.ToString();
                           flag=this.UptLevel_code(Maxid, Lcode);
                        }
                    }
                    if (flag)
                    {
                        trans.Complete();
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                }
            }

            return flag;
        }
        #endregion
        #region 修改用户等级
        public bool updateLevel(level model, string code)
        {

            bool flag = true;
            int agId = 0;

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    
                        
                        if (model.parent_id.ToString() != "0")
                        {
                            string Lcode = code + "00" + model.id.ToString();
                            flag = this.Update(model);
                        }
                        else
                        {
                            string Lcode = "00" + model.id.ToString();
                            flag = this.Update(model);
                        }
                    
                    if (flag)
                    {
                        trans.Complete();
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                }
            }

            return flag;
        }
        #endregion
        #endregion  ExtensionMethod
    }
}

